package com.cooba.activitys.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cooba.R;
import com.cooba.utils.ServerURL;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;

/**
 * Created by Administrator on 2015/12/16.
 * 忘记密码
 */
public class ForgetPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton loginimage_back;
    private Button mNextbutton;
    private TextView mBt_getNumber;
    private EditText phoneNumber_et;
    private EditText check_number;
    private String phoneNumber;
    private String codeNumber;

    public static ForgetPasswordActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        instance=this;
        PushAgent.getInstance(this).onAppStart();

        loginimage_back= (ImageButton) findViewById(R.id.loginimage_back);
        phoneNumber_et = (EditText) findViewById(R.id.etNumber);
        check_number = (EditText) findViewById(R.id.check_number);
        mBt_getNumber = (TextView) findViewById(R.id.bt_getNumber);
        mNextbutton = (Button) findViewById(R.id.nextbutton);
        mNextbutton.setOnClickListener(this);
        mBt_getNumber.setOnClickListener(this);
        loginimage_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        phoneNumber = phoneNumber_et.getText().toString().trim();

        codeNumber = check_number.getText().toString().trim();
        switch (v.getId()) {
            case R.id.loginimage_back:
                finish();
                break;
            case R.id.bt_getNumber:
                if (phoneNumber == null || phoneNumber.equals("")) {
                    Toast.makeText(getApplication(), "手机号码不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }
                //关闭获取
                mBt_getNumber.setEnabled(false);
                //60S倒计时
                new CountDownTimer(60000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        mBt_getNumber.setText((millisUntilFinished / 1000) + "秒后重发");
                    }

                    @Override
                    public void onFinish() {
                        mBt_getNumber.setText("获取验证码");
                        mBt_getNumber.setEnabled(true);
                    }
                }.start();
                // TODO: 2015/12/11  获取验证码

                params.addBodyParameter("phone", phoneNumber);
                httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.GetSendCode, params, new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Log.e("wei", "获取验证码失败");
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String msg = null;
                        try {
                            msg = JSON.parseObject(responseInfo.result).getString("msg");
                            String status = JSON.parseObject(responseInfo.result).getString("status");
                            Log.e("wei", "验证码获取成功msg:" + msg + ">>status=" + status);
                            if (status.equals("0")) {
                                Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                });
                break;
            case R.id.nextbutton:
                //校验验证码
                if (phoneNumber.length() > 0 && codeNumber.length() > 0) {
                    params.addBodyParameter("phone", phoneNumber);
                    params.addBodyParameter("send_code", codeNumber);

                    httpUtils.send(HttpRequest.HttpMethod.POST, ServerURL.httpUrl + ServerURL.CheckSendCode, params, new RequestCallBack<String>() {
                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            Log.e("sss", "获取失败");
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            try {
                                String msg = JSON.parseObject(responseInfo.result).getString("msg");
                                String status = JSON.parseObject(responseInfo.result).getString("status");
                                if (status.equals("1")) {
                                    Intent intent = new Intent(getApplication(), SetPasswordActivity.class);
                                    intent.putExtra("phone",phoneNumber);
                                    startActivity(intent);
                                } else {
                                    Toast.makeText(getApplication(), "验证码错误!", Toast.LENGTH_SHORT).show();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }else {
                    if (phoneNumber.length() == 0) {
                        Toast.makeText(getApplication(), "请填写手机号", Toast.LENGTH_SHORT).show();
                    }
                    else if (codeNumber.length() == 0) {
                        Toast.makeText(getApplication(), "请填写验证码", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
           default:break;
        }
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
